/**
 * 获取昨天、今天、明天
 * @param {Object} type 'prev'、'next'
 */
function getCurrentDay(type) {
	let date, year, month, day
	date = new Date()
	if (type === 'prev') {
		date.setTime(date.getTime() - 24 * 60 * 60 * 1000)
	}
	if (type === 'next') {
		date.setTime(date.getTime() + 24 * 60 * 60 * 1000)
	}
	year = date.getFullYear()
	month = date.getMonth() + 1
	day = date.getDate()
	if (month < 10) {
		month = '0' + month
	}
	if (day < 10) {
		day = '0' + day
	}
	return year + '-' + month + '-' + day
}

/**
 * 根据日期获取星期几
 * @param {String} date 2022-02-22
 */
function getCurrentWeek(date) {
	let [year, month, day] = date.split('-')
	let week = new Date(year, parseInt(month - 1), day).getDay()
	return '星期' + '日一二三四五六'.charAt(week)
}

/**
 * 根据日期获取上一个月、下一个月日期
 * @param {Object} type 'prev'、'next'
 * @param {Object} date '2022-02-22'
 */
function getCurrentMonth(type, dateStr) {
	let year, month, day, days
	if (!dateStr) {
		let date = new Date()
		year = date.getFullYear()
		month = date.getMonth() + 1
		day = date.getDate()
	} else {
		let dateArr = dateStr.split('-')
		year = dateArr[0]
		month = dateArr[1]
		day = dateArr[2]
	}
	if (type === 'prev') {
		month = parseInt(month) - 1
		if (month === 0) {
			year = parseInt(year) - 1
			month = 12
		}
	}
	if (type === 'next') {
		month = parseInt(month) + 1
		if (month === 13) {
			year = parseInt(year) + 1
			month = 1
		}
	}
	days = new Date(year, month, 0).getDate() //获取当前日期中的月的天数
	if (day > days) {
		day = days
	}
	if (month < 10) {
		month = '0' + month
	}
	if (day < 10) {
		day = '0' + day
	}
	return year + '-' + month + '-' + day
}


/**
 * 时间显示多久以前
 * @param {String} date 2022-02-22、2022-02-22 12:22:22
 */
function friendlyDate(date) {
	if ((date + '').length === 10) {
		date = date + ' 00:00:00'
	}
	let timestamp = new Date(date).getTime()
	let formats = {
		year: '%n% 年前',
		month: '%n% 个月前',
		day: '%n% 天前',
		hour: '%n% 小时前',
		minute: '%n% 分钟前',
		second: '%n% 秒前'
	}
	let now = Date.now()
	let seconds = Math.floor((now - timestamp) / 1000)
	let minutes = Math.floor(seconds / 60)
	let hours = Math.floor(minutes / 60)
	let days = Math.floor(hours / 24)
	let months = Math.floor(days / 30)
	let years = Math.floor(months / 12)
	let diffType = ''
	let diffValue = 0
	if (years > 0) {
		diffType = 'year'
		diffValue = years
	} else if (months > 0) {
		diffType = 'month'
		diffValue = months
	} else if (days > 0) {
		diffType = 'day'
		diffValue = days
	} else if (hours > 0) {
		diffType = 'hour'
		diffValue = hours
	} else if (minutes > 0) {
		diffType = 'minute'
		diffValue = minutes
	} else {
		diffType = 'second'
		diffValue = seconds || 1
	}
	return formats[diffType].replace('%n%', diffValue)
}
